<?php
namespace App\Services\ThirdPayments\Recharge;

use App\Lib\PaymentMap;
use App\Lib\ThirdPartyMap;
use App\Models\RechargeOrder;
use App\Services\ThirdPayments\Contracts\Actions\HrefRedirectAction;
use App\Services\ThirdPayments\Contracts\RechargeBaseAbstract;
use App\Services\ThirdPayments\Contracts\WAPable;

class WSJFPayment extends RechargeBaseAbstract implements WAPable
{
    protected $gw_pay = 'https://gate.7841.net/API/PostOrderHandler.ashx';
    protected $gw_query = 'https://gate.7841.net/API/SearchOrderHandler.ashx';

    public function callback(array $data)
    {
        // TODO: Implement callback() method.
        if ($this->veryCallbackSign($data)) {
            if ($data['opstate'] == 0) return true;
        }
        return false;
    }

    public function veryCallbackSign(array $data)
    {
        // TODO: Implement veryCallbackSign() method.
        $params = array_filter($data, function ($value) {
            if (strlen($value) == 0) return false;
            return true;
        });
        unset($params['sign']);
        ksort($params);
        $signStr = "";
        foreach ($params as $key => $param) {
            $signStr .= $key . '='. $param . '&';
        }
        $signStr = substr($signStr, 0, -1);
        $signStr .= $this->getMchKey();
        if (strcasecmp($data['sign'], md5($signStr)) == 0) return true;
        return false;
    }

    public function getOrderNoFromCallback(array $data)
    {
        // TODO: Implement getOrderNoFromCallback() method.
        return $data['orderid'];
    }

    public function getOrderAmtFromCallback(array $data)
    {
        // TODO: Implement getOrderAmtFromCallback() method.
        return $data['ovalue'];
    }

    public function getThirdNoFromCallback(array $data)
    {
        return $data['sysorderid'];
    }

    /**
     * @return mixed
     */
    public function getIdentify(): string
    {
        // TODO: Implement getIdentify() method.
        return ThirdPartyMap::WSJF;
    }

    /**
     *  needs child to achieve
     */
    public function initPaymentMap(): array
    {
        // TODO: Implement initPaymentMap() method.
        return [
            PaymentMap::WX => '310',
            PaymentMap::WX_WAP => '330',
            PaymentMap::QQ => '410',
            PaymentMap::QQ_WAP => '421',
            PaymentMap::ALI => '210',
            PaymentMap::ALI_WAP => '220'
        ];
    }

    public function query(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement query() method.
        $this->setParameter('merchant', $this->getMchId());
        $this->setParameter('orderid', $rechargeOrder->plat_no);
        $this->setParameter('sign', $this->querySign());
        $res = curlHttp($this->getGwQuery(), $this->getParameters());
        if ($res['http_code'] == 200) {
            $body = json_decode($res['body'], true);
            if ($body['result'] == 'OK') {
                if ($body['status'] == 2) return true;
                return '未支付';
            }
            return $body['msg'];
        }
        return $res['body'];
    }

    public function bankHref(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement bankHref() method.
    }

    public function scanCode(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement scanCode() method.
        $this->initParameters($rechargeOrder);
        return $this->getGwPay() . '?' . http_build_query($this->getParameters());
    }

    public function paySign(): string
    {
        // TODO: Implement paySign() method.
        $params = $this->getParameters();
        ksort($params);
        $signStr = "";
        foreach ($params as $key => $param) {
            $signStr .= $key . '='. $param . '&';
        }
        $signStr = substr($signStr, 0, -1);
        $signStr .= $this->getMchKey();
        return md5($signStr);
    }

    public function querySign(): string
    {
        // TODO: Implement querySign() method.
        $signStr = "";
        $signStr .= "merchant=" . $this->getParameter('merchant') . '&';
        $signStr .= "orderid=" . $this->getParameter('orderid');
        $signStr .= $this->getMchKey();
        return md5($signStr);
    }

    public function showSuccess(): string
    {
        // TODO: Implement showSuccess() method.
        return 'OK';
    }

    public function initParameters(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement initParameters() method.
        $mchData = $rechargeOrder->order_data;
        $this->setParameter('callbackurl', $this->getCallbackUrl());
        $this->setParameter('hrefbackurl', $this->getReturnUrl());
        $this->setParameter('orderid', $rechargeOrder->plat_no);
        $this->setParameter('merchant', $this->getMchId());
        $this->setParameter('type', $this->getPaymentMap($mchData['recharge_type']));
        $this->setParameter('value', $rechargeOrder->order_amt);
        $this->setParameter('sign', $this->paySign());
    }

    public function wapReq(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement wapReq() method.
        $this->initParameters($rechargeOrder);
        return new HrefRedirectAction($this->getGwPay() . '?' . http_build_query($this->getParameters()));
    }
}
